﻿Ext.define('app.prm.project_task.TaskInfo', {
    extend: 'Ext.grid.Panel',
    border: false,
    viewConfig: {
        enableTextSelection: true //可以复制单元格文字
    },
    projectId: 0,
    initComponent: function () {
        var me = this;
        me.store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/projectTask/GetTasks', filter: { projectId: me.projectId } });

        me.tbar = [
            {
                text: '新增问题',
                glyph: 0xf067,
                bind: {
                    hidden: '{!hasAddTask}'
                },
                handler: function () {
                    //var win = Ext.create("app.prm.project_task.EditTask", { projectId: me.projectId, title: '新增问题' });
                    //win.on("savesuccess", function () {
                    //    me.store.load();
                    //    win.close();
                    //});

                    var win = Ext.create("app.prm.project_task.EditQuan", {
                        projectId: me.projectId,
                        title: '新增问题',
                        IsAdd: true,
                        IsEdit: true
                    });

                    win.on("savesuccess", function () {
                        me.store.load();
                        win.close();
                    });

                    win.show();
                }
            },
            {
                text: '导入问题',
                glyph: 0xf067,
                bind: {
                    hidden: '{!hasAddTask}'
                },
                handler: function () {
                    var win = Ext.create("app.prm.project_task.ImportTask", { projectId: me.projectId });
                    win.on("savesuccess", function () {
                        me.store.load();
                        win.close();
                    });
                    win.show();
                }
            },
            {
                text: '导出Word',
                glyph: 0xf08e,
                bind: {
                    hidden: '{!hasTaskExport}'
                },
                handler: function () {
                    var json = me.searchPanel.getForm().getValues();
                    window.location.href = "/api/services/app/projectTask/GetExport?projectId=" + me.projectId
                     + "&Route=" + json.route
                     + "&Dept=" + json.dept
                     + "&StartTime=" + json.startTime
                     + "&EndTime=" + json.endTime
                     + "&Tags=" + json.tags;
                }
            },
            //{
            //    text: '导出Pdf',
            //    glyph: 0xf08e,
            //    bind: {
            //        hidden: '{!hasTaskExport}'
            //    },
            //    handler: function () {
            //        window.location.href = "/api/services/app/projectTask/GetExport?projectId=" + me.projectId + "&format=40";
            //    }
            //},
            {
                text: '保存修改',
                glyph: 0xf0c7,
                handler: function () {
                    var sels = me.store.getUpdatedRecords();
                    if (sels.length == 0) {
                        Ext.Msg.alert('系统提示', '没有修改的数据');
                        return false;
                    }

                    var records = [];
                    for (var i = 0; i < sels.length; i++) {
                        var r = sels[i];
                        records.push({ Id: r.data.id, IsFinsh: r.data.isFinsh, Priority: r.data.priority, IsArchived: r.data.isArchived, IsFollower: r.data.isFollower, PlanFinshTime: r.data.planFinshTime });
                    }

                    App.AjaxPost('/api/services/app/projectTask/BatchEditTask', { datas: records }, function () {
                        me.loadGrid();
                    });
                }
            },
            "->",
            {
                text: '查询', glyph: 0xf002, menu:
                {
                    xtype: 'menu',
                    plain: true,
                    items: me.getSearchPanel()
                }
            },
            {
                text: '刷新',
                glyph: 0xf021,
                handler: function () {
                    this.ownerCt.ownerCt.store.reload();
                }
            }
        ];


        me.finshCheck = new Ext.grid.column.Check({
            header: 'F (完成)', maxWidth: 30, width: 30, align: 'left', dataIndex: 'isFinsh',
            listeners: {
                beforecheckchange: function (column, rowIndex, checked, eOpts) {
                    var record = me.store.getAt(rowIndex);
                    if (!record.data.isMember) {
                        return false;
                    }
                }
            }
        });

        me.archivedCheck = new Ext.grid.column.Check({
            header: 'C (归档)', maxWidth: 30, width: 30, align: 'left', dataIndex: 'isArchived',
            listeners: {
                beforecheckchange: function (column, rowIndex, checked, eOpts) {
                    var record = me.store.getAt(rowIndex);
                    if (!record.data.hasTaskArchived) {
                        return false;
                    }
                }
            }
        });

        me.archivedFollower = new Ext.grid.column.Check({
            header: 'T (关注)', maxWidth: 30, width: 30, align: 'left', dataIndex: 'isFollower',
            listeners: {
                beforecheckchange: function (column, rowIndex, checked, eOpts) {
                    var record = me.store.getAt(rowIndex);
                    if (!record.data.hasTaskFollower) {
                        return false;
                    }
                }
            }
        });

        me.columns = [
        {
            xtype: 'actioncolumn',
            maxWidth: 60,
            header: '操作',
            items: [
            {
                iconCls: 'search',
                tooltip: '任务详细',
                handler: function (grid, rowIndex, colIndex) {
                    var record = grid.store.getAt(rowIndex);
                    var win = Ext.create("app.prm.project_task.EditQuan", {
                        projectTaskId: record.data.id,
                        projectId: record.data.projectId,
                        title: '编辑问题',
                        IsAdd: false,
                        IsEdit: record.data.isMember,
                        IsFollower: record.data.hasTaskFollower,
                        是否已关注: record.data.isFollower
                    });
                    win.on("savesuccess", function () {
                        grid.store.load();
                        win.close();
                    });
                    win.show();
                }
            },
            //{
            //    iconCls: 'pageword',
            //    tooltip: '答案',
            //    handler: function (grid, rowIndex, colIndex) {
            //        var record = grid.store.getAt(rowIndex);
            //        var win = Ext.create("app.prm.project_task.TaskReply", { projectTaskId: record.data.id });
            //        win.show();
            //    },
            //    getClass: function (v, meta, record) {
            //        if (record.data.isMember) {
            //            return 'pageword';
            //        } else {
            //            return 'x-hidden';
            //        }
            //    }
            //},
            //{
            //    iconCls: 'edit',
            //    tooltip: '编辑',
            //    handler: function (grid, rowIndex, colIndex) {
            //        var record = grid.store.getAt(rowIndex);
            //        var win = Ext.create("app.prm.project_task.EditTask", { projectTaskId: record.data.id, projectId: record.data.projectId, title: '编辑问题' });
            //        win.on("savesuccess", function () {
            //            grid.store.load();
            //            win.close();
            //        });
            //        win.show();
            //    },
            //    getClass: function (v, meta, record) {
            //        if (record.data.isMember) {
            //            return 'edit';
            //        } else {
            //            return 'x-hidden';
            //        }
            //    }
            //},
            {
                iconCls: 'delete',
                tooltip: '删除问题',
                handler: function (grid, rowIndex, colIndex) {
                    var record = grid.store.getAt(rowIndex);
                    Ext.Msg.confirm('系统提示', '确定要删除问题吗？',
                    function (btn) {
                        if (btn == 'yes') {
                            App.AjaxDelete('/api/services/app/projectTask/DeleteTask?projectTaskId=' + record.data.id, function () {
                                grid.store.remove(record);
                            });
                        }
                    });
                },
                getClass: function (v, meta, record) {
                    if (record.data.isMember) {
                        return 'delete';
                    } else {
                        return 'x-hidden';
                    }
                }
            }
            ]
        },
        { xtype: 'rownumberer', text: '#' },
        me.finshCheck,
        me.archivedCheck,
        me.archivedFollower,
        {
            header: '！', maxWidth: 50, align: 'center', dataIndex: 'priority', editor: {
                xtype: 'numberfield',
                allowDecimals: false
            }
        },
        {
            header: '问题', dataIndex: 'title', renderer: function (value, p, record) {
                p.tdAttr = 'data-qtip="' + value + '"';

                var subTask = record.get('subTask');
                if (parseInt(subTask) > 0) {
                    var finshSubTask = record.get('finshSubTask');
                    return '【' + finshSubTask + '/' + subTask + '】' + value;
                }
                else {
                    return value;
                }

            }
        },
        { header: '工艺路线', maxWidth: 100, hidden: true, dataIndex: 'route' },
        { header: '步骤', maxWidth: 100, hidden: true, dataIndex: 'step' },
        { header: '所属', maxWidth: 100, hidden: true, dataIndex: 'dept' },
        //{
        //    header: '子任务', maxWidth: 80, dataIndex: 'subTask', renderer: function (value, p, record) {
        //        if (parseInt(record.data.subTask) > 0)
        //            return record.data.finshSubTask + "/" + record.data.subTask;
        //        else
        //            return "";
        //    }
        //},
        {
            header: '截止时间', maxWidth: 88, hidden: true, dataIndex: 'planFinshTime', renderer: function (value, p, record) {
                return Ext.util.Format.date(value, 'Y-m-d');
            },
            editor: { xtype: 'datefield', format: 'Y-m-d' }
        },
        {
            header: '成员', width: 80, maxWidth: 200, dataIndex: 'members', renderer: function (value, p, record) {
                p.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },
        {
            header: '标签', width: 80, hidden: true, maxWidth: 200, dataIndex: 'tags', renderer: function (value, p, record) {
                p.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }
        ];
        me.callParent();
    },
    forceFit: true,
    plugins: [
        Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1,
            listeners: {
                beforeedit: function (editor, context, eOpts) {
                    var record = context.record;
                    var field = context.field;
                    if (field == 'planFinshTime') {
                        if (!record.data.isMember) {
                            return false;
                        }
                    }
                    if (field == 'priority') {
                        if (!record.data.isMember) {
                            return false;
                        }
                    }
                }
            }
        })
    ],
    getSearchPanel: function () {
        var me = this;
        if (!this.searchPanel) {
            this.searchPanel = new Ext.form.Panel(
            {
                defaults: { anchor: '100%' },
                bodyStyle: 'padding:5px',
                defaultType: 'textfield',
                defaults: {
                    anchor: '100%'
                },
                fieldDefaults: {
                    labelWidth: 90,
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        fieldLabel: '工艺路线',
                        name: 'route'
                    },
                    {
                        fieldLabel: '所属',
                        name: 'dept'
                    },
                    {
                        xtype: 'datefield',
                        fieldLabel: '计划开始',
                        name: 'startTime'
                    },
                    {
                        xtype: 'datefield',
                        fieldLabel: '计划结束',
                        name: 'endTime'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '标签',
                        name: 'tags'
                    },
                    {
                        xtype: 'checkbox',
                        bind: {
                            hidden: '{!hasTaskArchived}'
                        },
                        name: 'ViewArchived',
                        inputValue: true,
                        fieldLabel: '查看归档'
                    }
                ],
                buttons: [
                    {
                        text: '开始搜索',
                        handler: function () {
                            me.loadGrid();
                        }
                    },
                    {
                        text: '清空条件',
                        handler: function () {
                            me.searchPanel.reset();
                            me.loadGrid();
                        }
                    }
                ]
            });
        }
        return this.searchPanel;
    },
    loadGrid: function () {
        var me = this;
        var json = me.searchPanel.getForm().getValues();
        json.projectId = me.projectId;
        me.store.filter = json;
        me.store.load();
    },
    listeners: {
        celldblclick: function (grid, td, cellIndex, record, tr, rowIndex, e, eOpts) {
            var filed = e.position.column.dataIndex;
            if (filed == 'members' || filed == 'title') {
                var record = grid.store.getAt(rowIndex);
                var win = Ext.create("app.prm.project_task.EditQuan", {
                    projectTaskId: record.data.id,
                    projectId: record.data.projectId,
                    title: '编辑问题',
                    IsAdd: false,
                    IsEdit: record.data.isMember,
                    IsFollower: record.data.hasTaskFollower,
                    是否已关注: record.data.isFollower
                });
                win.on("savesuccess", function () {
                    grid.store.load();
                    win.close();
                });
                win.show();
            }

        }
    }
})